package com.handpay.nocardpay.baselibrary.db.ormlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.main.all.model.bean.component.ComponentItemBean;
import com.handpay.nocardpay.baselibrary.main.all.model.bean.component.ComponentMappingItemBean;
import com.handpay.nocardpay.baselibrary.main.all.model.bean.component.ComponentRelationItemBean;
import com.handpay.nocardpay.baselibrary.notification.model.bean.NoticeBody;
import com.handpay.nocardpay.baselibrary.utils.AccountUtils;
import com.handpay.nocardpay.baselibrary.utils.PreferenceManager;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;

/**
 * @author sxshi on 2017/4/6.
 *         email:emotiona_xiaoshi@126.com
 *         describe:数据库操作
 */

public class DataBaseHelper extends OrmLiteSqliteOpenHelper {
    // 数据库名称
    private static final String DATABASE_NAME = "handpay.db";
    // 数据库版本
    private static int DB_VERSION = 7;
    private static DataBaseHelper instance;
    private Context mContext;

    private DataBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DB_VERSION);
        mContext = context;
    }

    public static synchronized DataBaseHelper getDBHelper(Context context) {
        context = context.getApplicationContext();
        if (instance == null) {
            synchronized (DataBaseHelper.class) {
                if (instance == null) {
                    instance = new DataBaseHelper(context);
                }
            }
        }
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
        try {
            TableUtils.createTable(connectionSource, NoticeBody.class);
            TableUtils.createTable(connectionSource, ComponentItemBean.class);
            TableUtils.createTable(connectionSource, ComponentMappingItemBean.class);
            TableUtils.createTable(connectionSource, ComponentRelationItemBean.class);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            TableUtils.dropTable(connectionSource, NoticeBody.class, true);
            TableUtils.dropTable(connectionSource, ComponentItemBean.class, true);
            TableUtils.dropTable(connectionSource, ComponentMappingItemBean.class, true);
            TableUtils.dropTable(connectionSource, ComponentRelationItemBean.class, true);
            onCreate(database, connectionSource);
//            String userId = AccountUtils.getUserId(mContext);
//            HPLog.i(HPLog.LFP_TAG, "onUpgrade -userId 1:" + userId);
//            if (TextUtils.isEmpty(userId)){
//                HPLog.i(HPLog.LFP_TAG, "onUpgrade -userId 2:" + userId);
//                return;
//            }
//            PreferenceManager.getInstance(mContext).remove(userId);
//            HPLog.i(HPLog.LFP_TAG, "onUpgrade -userId 3:" + userId);


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
